

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web {
  public partial class review : System.Web.UI.Page {

    #region Member Variables

    private int productId = 0;
    Product _product;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        productId = Utility.GetIntParameter("pid");
        if (productId > 0) {
          SetReviewProperties();
          _product = new Product(productId);
          lblName.Text = _product.Name;
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(review).Name + ".Page_Load", ex);
        throw;
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      try {
        if (!string.IsNullOrEmpty(txtTitle.Text) && !string.IsNullOrEmpty(txtReview.Text)) {
          Review review = new Review();
          review.ProductId = _product.ProductId;
          review.Rating = reviewRating.CurrentRating;
          review.Title = txtTitle.Text.Trim();
          review.Body = txtReview.Text;
          review.Save(WebUtility.GetUserName());
          pnlReview.Visible = false;
          LocalizationUtility.AddLabelText(lblReviewSaved);
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(review).Name + ".btnSave_Click", ex);
        throw;
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Sets the review properties.
    /// </summary>
    private void SetReviewProperties() {
      LocalizationUtility.AddLabelText(lblYourRating);
      LocalizationUtility.AddLabelText(lblTitle);
      LocalizationUtility.AddLabelText(lblReview);
      LocalizationUtility.AddButtonText(btnSave);
    }

    #endregion

    #endregion

  }
}
